<template>
	<view class="page-transfer">
		<view class="withdraw-alert text-highlight">
			<block v-if="symbol.toUpperCase() == 'USDT'">暂未开放USDT的提币</block>
			<block v-else>提现手续费5 SID</block>
		</view>
		<view class="form">
			<view class="item">
				<view class="label">收款地址(TRC20地址)</view>
				<view class="input-box"><input v-model="address" placeholder="钱包收款地址" /></view>
			</view>
			<view class="item">
				<view class="label">提币数量(当前余额:{{ balance }}{{ symbol.toUpperCase() }})</view>
				<view class="input-box"><input v-model="amount" placeholder="请输入数量" /></view>
			</view>
			<view class="alert-message" v-if="symbol.toUpperCase() == 'SID'">提现手续费5 SID</view>
			<view class="item">
				<view class="label">安全密码</view>
				<view class="input-box">
					<input class="password" password="" v-model="password" placeholder="请输入您的安全密码" />
				</view>
			</view>
		</view>
		<view class="button-box-padding"><button class="btn-block btn-primary btn" @click="submit">确认提现</button></view>
	</view>
</template>

<script>
	import api from "@/utils/api/wallet/index.js"
	import util from '@/common/util.js';

	export default {
		data() {
			return {
				gas: 0,
				password: "",
				symbol: "",
				balance: 0,
				amount: "",
				address: "",
				userAddress: "",
			}
		},
		onLoad(e) {
			this.symbol = e.symbol;
			this.balance = e.balance;
			uni.setNavigationBarTitle({
				title: this.symbol.toUpperCase() + "提现"
			})
			let user = util.getUser();
			this.userAddress = user.wallet_address;
			let cacheAddress = uni.getStorageSync("cache_address");
			if (typeof(cacheAddress) != "undefined") {
				this.address = cacheAddress;
			}
		},
		onNavigationBarButtonTap() {
			this.scanAddressQrcode();
		},
		methods: {
			scanAddressQrcode() {
				let that = this;
				uni.scanCode({
					success: res => {
						let address = res.result;
						if (address == that.userAddress) {
							util.showError('不能给提现给自己钱包');
							return;
						} else {
							that.address = address
						}
					}
				});
			},
			async submit() {
				let checkAddress = util.checkItem("wallet_address", this.address)
				let checkPassword = util.checkItem("password", this.password)
				let checkAmount = util.checkItem("amount", this.amount)
				if (!checkAddress.status) {
					util.showError(checkAddress.message);
					return
				}
				if (!checkAmount.status) {
					util.showError(checkAmount.message);
					return
				}
				if (!checkPassword.status) {
					util.showError(checkPassword.message);
					return
				}
				if (this.address == this.userAddress) {
					util.showError("不能给自己提现")
					return;
				}
				if (parseFloat(this.balance) - parseFloat(this.amount) < 0) {
					util.showError("最多提现数量" + this.balance)
					return;
				}
				let post = {
					symbol: this.symbol,
					amount: this.amount,
					password: this.password,
					address: this.address
				};
				uni.setStorageSync("cache_address", this.address);
				let res = await api.withdraw(post)
				if (res.status == 1) {
					uni.reLaunch({
						url: "withdraw-sucess?data=" + JSON.stringify(res.data)
					})
					// uni.navigateTo({
					// 	url:
					// })
				}
			}
		}
	}
</script>

<style lang="scss">
	.alert-message {
		font-size: 12px;
		padding-bottom: 10px;
		text-align: right;
		color: #31E8D6;
	}
	.withdraw-alert{
		padding:5px 15px;
		font-size: 12px;
	}
</style>
